package com.cz.ssyx.common.result;

import lombok.Data;

/**
 * @descriptions:
 * @author: Joker.
 * @date: 2024/4/13 19:48
 * @version: 1.0
 */

@Data
public class Result<T> {
    //状态码
    private Integer code;

    //信息
    private String message;

    //数据
    private T data;

    //构造私有化
    private Result() {}

    //设置数据,返回对象的方法
    public static<T> Result<T> build(T data, ResultCodeEnum resultCodeEnum) {
        Result<T> result = new Result<>();

        if (data != null){
            result.setData(data);
        }
        result.setCode(resultCodeEnum.getCode());
        result.setMessage(resultCodeEnum.getMessage());
        return result;
    }

    public static<T> Result<T> build(T data, Integer code, String message) {
        Result<T> result = new Result<>();

        if (data != null){
            result.setData(data);
        }
        result.setCode(code);
        result.setMessage(message);
        return result;
    }

    //创建成功的方法
    public static<T> Result<T> ok(T data) {
        return build(data, ResultCodeEnum.SUCCESS);
    }

    //创建失败的方法
    public static<T> Result<T> fail(T data) {
        return build(data, ResultCodeEnum.FAIL);
    }
}
